0

Hi i have this code that fetch and returns json data from file config.json

text.js

 class Text extends React.Component {
     constructor(props){
        super(props)
        this.state = {
          datat: [],
        };
      }
      componentDidMount(){
         fetch('/config.json')
          .then(response => response.json())
          .then((datao) =>{        
            this.setState({
                datat: (JSON.parse(JSON.stringify(datao)))
            })

          });
       }
      render(){
         const datatorender = this.state.datat;
    return ( Object.keys(datatorender).map(key =>{if(key==this.props.value){return datatorender[this.props.value]}}))
        }}

and how i call it in home is like : home.js

<Text value="SITENAME">

so i want to call it like this : {text.SITENAME} instead of fist one how can i do that ?

and this is the json file :

{
  "SITENAME": "site name",
  "SITE_DESCRIPTION":"desc"
}

4
  • What I understand is you want to access the fetched value "site name" in the parent component of home.js by calling something like {text.SITENAME}, right? Commented May 13, 2019 at 3:58
  • @Faisal Rahman Avash yes Commented May 13, 2019 at 10:47
  • I don't think you can do that. The closest way to imitate this behavior is by keeping the state in the parent component and passing a callback as prop to Text component so that the parent state can be updated with that callback. Are you familiar with this approach? Commented May 13, 2019 at 18:42
  • Yes i like that how can i do it :D Commented May 13, 2019 at 19:01

2 Answers 2

1

Try this:

class Text extends React.Component {
     constructor(props){
        super(props)
        this.props = props;
        this.state = {
          datat: [],
        };
      }
      componentDidMount(){
         fetch('/config.json')
          .then(response => response.json())
          .then((datao) =>{        
            this.setState({
                datat: (JSON.parse(JSON.stringify(datao)))
            })

          });
       }
      render() {
          const datatorender = this.state.datat;
          console.log(datatorender)
          return (
            <div>
              {
                Object.keys(datatorender).map((key, i) => {
                  if (key === this.props.value) {
                    return (
                      <li key={i}> {datatorender[this.props.value]} </li>
                    )
                  } else {
                    return null
                  }
                })
              }
            </div>
          )
      }
}

Sign up to request clarification or add additional context in comments.

Comments

0

Here's how you do it:

// FILE: home.js
class Home extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      datat: [],
    };
  }
  render() {
    return 
    <div>
      {this.state.datat.SITENAME === undefined ? null : this.state.datat.SITENAME} // Just like you wanted to access
      <Text value="SITENAME"
            datat={this.state.datat}
            updateDatat={(val) => this.setState({datat: val})}/>
    </div>
  }
}

// FILE: text.js 
class Text extends React.Component {
  componentDidMount() {
    fetch('/config.json')
      .then(response => response.json())
      .then((datao) => {
        this.props.updateDatat({
          JSON.parse(JSON.stringify(datao))
        })
      });
  }
  render() {
    const datatorender = this.props.datat;
    return (Object.keys(datatorender).map(key => {
      if (key == this.props.value) {
        return datatorender[this.props.value]
      }
    }))
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.