CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2022-09-02
by Hans Ott

Original Post

Original - Posted on 2014-07-02
by Douglas



            
Present in both answers; Present only in the new answer; Present only in the old answer;

I would avoid using `dangerouslySetInnerHTML`.
You can do something like:
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
function SpecialWord({ children }) { return <strong>{children}</strong>; }
function Component() { const segmenter = new Intl.Segmenter([], { granularity: 'word' }); const parts = Array.from(segmenter.segment("This is my text Albert is my special word")).map(part => part.segment);
return parts.map((part, index) => { if (part === "Albert") { return <SpecialWord key={index}>Albert</SpecialWord>; }
return part; }) }
ReactDOM.render( <Component />, document.getElementById("root") );
<!-- language: lang-html -->
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<!-- end snippet -->
A couple of notes: - You need a way to split the sentence in an array, so that you can use `map` over the words of the sentence and output a React component for the special word. I used `Intl.Segmenter` which is fairly new and might not be supported on older browsers. Also, you might want to match things like "special word". `Intl.Segmenter` splits in words so you won't be able to match multiple words. - I used `key={index}`, using an index as key is often a mistake. In this case it works because we don't have a unique ID for each word. - Instead of passing the special word as child, it's better to use a property for this: `<SpecialWord word={part} />` (React children are kinda hard to work with)
I hope this helps!
React circa 2020 ----------------
In the `onClick` callback, call the [state hook's][1] setter function to update the state and re-render:
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
const Search = () => { const [showResults, setShowResults] = React.useState(false) const onClick = () => setShowResults(true) return ( <div> <input type="submit" value="Search" onClick={onClick} /> { showResults ? <Results /> : null } </div> ) }
const Results = () => ( <div id="results" className="search-results"> Some Results </div> )
ReactDOM.render(<Search />, document.querySelector("#container"))
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>
<div id="container"> <!-- This element's contents will be replaced with your component. --> </div>
<!-- end snippet -->
[JSFiddle][2]
React circa 2014 ----------------
The key is to update the state of the component in the click handler using `setState`. When the state changes get applied, the `render` method gets called again with the new state:
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
var Search = React.createClass({ getInitialState: function() { return { showResults: false }; }, onClick: function() { this.setState({ showResults: true }); }, render: function() { return ( <div> <input type="submit" value="Search" onClick={this.onClick} /> { this.state.showResults ? <Results /> : null } </div> ); } });
var Results = React.createClass({ render: function() { return ( <div id="results" className="search-results"> Some Results </div> ); } });
ReactDOM.render( <Search /> , document.getElementById('container'));
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.2/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/15.6.2/react-dom.min.js"></script>
<div id="container"> <!-- This element's contents will be replaced with your component. --> </div>
<!-- end snippet -->
[JSFiddle][3]

[1]: https://reactjs.org/docs/hooks-state.html [2]: https://jsfiddle.net/khx30pnv/ [3]: http://jsfiddle.net/kb3gN/15084/

        
Present in both answers; Present only in the new answer; Present only in the old answer;